package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.modules.fsd.entity.FsdAuth;
import com.scs.application.modules.fsd.mapper.FsdAuthMapper;
import com.scs.application.modules.fsd.service.FsdAuthService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 */
@Service
public class FsdAuthServiceImpl extends BaseServiceImpl<FsdAuthMapper,FsdAuth> implements FsdAuthService {

    @Override
    public Boolean saveAuth(String stationCode, String userId) {
        FsdAuth auth = new FsdAuth();
        auth.setStationCode(stationCode);
        auth.setUserId(userId);
        auth.setStartTime(DateUtils.nowDate());
        save(auth);
        return true;
    }

    @Override
    public Boolean saveUnAuth(String stationCode, String userId) {
        FsdAuth auth = this.getOne(Wrappers.<FsdAuth>query().eq("station_code",stationCode).eq("user_id",userId).isNull("end_time").orderByDesc("start_time").last("limit 1"));
        if(auth==null){
            return false;
        }
        auth.setEndTime(DateUtils.nowDate());
        updateById(auth);
        return true;
    }

    @Override
    public List<Map<String, Object>> getUserAuthLogByStationCode(String stationCode) {
        return baseMapper.getUserAuthLogByStationCode(stationCode);
    }
}
